<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input clearable @keyup.enter.native="handleFind" class="filter-item" v-model="keyword" placeholder="请输入用户名"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="handleFind">查找</el-button>
      <el-button type="danger" @click="handleDeleteBatch" icon="el-icon-delete">强踢选中</el-button>
    </div>
    <el-table :data="tableData" v-loading="Loading" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection"></el-table-column>

      <el-table-column label="序号" width="60" align="center">
        <template slot-scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.userName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="角色" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.roleName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="ip" min-width="150" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.ipaddr }}</span>
        </template>
      </el-table-column>

      <el-table-column label="登录地点" align="center">
        <template slot-scope="scope">
          <el-tag type="warning">{{ scope.row.loginLocation }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作系统" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.os }}</span>
        </template>
      </el-table-column>

      <el-table-column label="浏览器" min-width="200" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.browser }}</span>
        </template>
      </el-table-column>

      <el-table-column label="登录时间" min-width="160" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.loginTime }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" fixed="right" align="center" min-width="160">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">强踢</el-button>
        </template>
      </el-table-column>
    </el-table>

  </div>
</template>

<script>
import {
  getOnlineAdminList,
  forceLogout
} from "@/api/admin";
export default {
  data() {
    return {
      Loading: true,
      multipleSelection: [], //多选，用于批量删除
      tableData: [],
      keyword: ""
    }
  },
  created() {
    this.onlineAdminList();
  },
  methods: {

    onlineAdminList() {
      this.Loading = true;
      var params = {};
      params.keyword = this.keyword;
      getOnlineAdminList(params).then(response => {
        this.tableData = response.data;
        this.currentPage = response.data.current;
        this.pageSize = response.data.size;
        this.total = response.data.total;
        this.Loading = false;
      });
    },
    handleFind() {
      this.onlineAdminList();
    },
    handleDelete(row) {
      this.$confirm("此操作将把用户踢下线, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var params = [];
          params.push(row.tokenId);
          forceLogout(params).then(response => {
              this.$message({
                type: "success",
                message: response.data
              });
            this.onlineAdminList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    handleDeleteBatch() {
      let tokenList = []
      let multipleSelection = this.multipleSelection;
      if (multipleSelection.length <= 0) {
        this.$message({
          type: "error",
          message: "请先选中需要踢出的用户！"
        });
        return;
      } else {
        for (let a = 0; a < multipleSelection.length; a++) {
          tokenList.push(multipleSelection[a].tokenId)
        }
      }
      this.$confirm("此操作将把选中的用户踢出, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          forceLogout(tokenList).then(response => {
              this.$message({
                type: "success",
                message: response.data
              });
            this.onlineAdminList();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.onlineAdminList();
    },
    // 改变多选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    }
  },
}
</script>